package com.mbbb.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbbb.yygh.common.result.R;
import com.mbbb.yygh.hosp.service.HospitalSetService;
import com.mbbb.yygh.model.hosp.HospitalSet;
import com.mbbb.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author mbbb
 * @since 2021-12-01
 */
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospital-set")
//@CrossOrigin //解决跨域问题
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //9.医院设置锁定和解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //8.批量删除
    //  [1,2,3]
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Long> idList){
        //传递集合  [1,2,3]
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }

    //7.根据id修改
    @ApiOperation("根据id修改")
    @PostMapping("/updateHospSetById")
    public R updateHospSetById(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //6.根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("/getHospSetById/{id}")
    public R getHospSetById(@PathVariable Long id){
        //TODO 模拟异常
       /* try {
            int i = 1/0;
        } catch (Exception e) {
            throw new YyghException(20001,"执行自定义异常处理");
        }*/
        HospitalSet byId = hospitalSetService.getById(id);
        return R.ok().data("byId",byId);
    }

    //5.添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospSet")
    public R saveHospSet(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    //4.条件查询带分页
    // @RequestBody
    // 提交json数据，使用注解获取json数据进行封装
    // post提交一起使用  get提交使用得不到数据， 其他提交方式delete put数据不完整
    @ApiOperation("条件查询带分页")
    @PostMapping("/selectPageQuery/{current}/{limit}")
    //@GetMapping("/selectPageQuery/{current}/{limit}")
    public R selectPageQuery(@PathVariable Long current,
                             @PathVariable Long limit,
                             @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建分页对象，传递分页参数
        Page<HospitalSet> objectPage = new Page<>(current,limit);
        //如果查询条件为空，则查询全部
        if (hospitalSetQueryVo==null){
            //调用service方法
            hospitalSetService.page(objectPage);
        } else { //如果查询条件不为空
            //构建条件
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();
            //判断非空
            QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(hoscode))
                //封装条件
                wrapper.eq("hoscode",hoscode);
            if (!StringUtils.isEmpty(hosname))
                //封装条件
                wrapper.like("hosname",hosname);
            //调用方法
            hospitalSetService.page(objectPage, wrapper);
        }
        // 返回结果
        long total = objectPage.getTotal();
        List<HospitalSet> records = objectPage.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //3.分页查询
    @ApiOperation("分页查询")
    @GetMapping("/selectPage/{current}/{limit}")
    public R selectPage(@PathVariable Long current,Long limit){
        //创建分页对象，传递分页参数
        Page<HospitalSet> objectPage = new Page<>(current,limit);
        //调用service方法
        hospitalSetService.page(objectPage,null);
        long total = objectPage.getTotal();
        List<HospitalSet> records = objectPage.getRecords();
        //返回结果
        return R.ok().data("total",total).data("records",records);
    }

    //2.删除医院设置（逻辑删除）
    //get提交浏览器可以直接访问测试，delete不能直接访问
    //http://localhost:8201/hosp/hospital-set/remove/1
    //http://localhost:8201/swagger-ui.html
    @ApiOperation("删除医院设置")
    @DeleteMapping("/remove/{id}")
    public R removeHospSet(@PathVariable Long id){
        //调用service方法
        boolean isRemove = hospitalSetService.removeById(id);
        if (isRemove){
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }


    //1.查询所有医院设置数据
    //http://localhost:8201/hosp/hospital_set/findAll
    @ApiOperation("医院设置列表")
    @GetMapping("/findAll")
    public R getAllHospSet(){
        //调用service方法
        List<HospitalSet> list = hospitalSetService.list();

        //封装返回结果对象
        return R.ok().data("list",list);
    }
}

